/**
 * 
 */
package co.fitstart.admin.handler.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.support.Result;
import com.demo2do.core.support.sms.handler.SmsHandler;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.admin.entity.MessageType;
import co.fitstart.admin.handler.SmsSender;
import co.fitstart.entity.Constant;
import co.fitstart.entity.course.round.CourseRound;

/**
 * @author downpour
 *
 */
@Component("smsSender")
public class SmsSenderImpl implements SmsSender {

	private static final Logger logger = LoggerFactory.getLogger(SmsSender.class);

	@Autowired
	private SmsHandler smsHandler;

	@Value("#{config['sms.company']}")
	private String company;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.weapp.api.handler.SmsSender#generateVerifyCode(java.lang.
	 * String)
	 */
	public String generateVerifyCode(String mobile) {
		return RandomStringUtils.randomNumeric(6);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.fitstart.admin.handler.SmsSender#sendCode(java.lang.String)
	 */
	@Override
	public boolean sendCode(String mobile, String code) {

		Result httpResult = new Result();
		
		// Send message
		httpResult = smsHandler.sendShortMessage(mobile, "【人马君】您的验证码是" + code);

		if (!httpResult.isValid()) {
			logger.error("sendCode() error", JsonUtils.toJsonString(httpResult));
		}
		
		return httpResult.isValid();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.fitstart.admin.handler.SmsSender#sendOrderCoachNotice(java.lang.
	 * String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void sendOrderCoachNotice(String mobile, String student, String courseName, String studentMobile) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.NEWSTUDENT_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("user", student);
			values.put("courseName", courseName);
			values.put("studentMobile", studentMobile);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendOrderCoachNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendCourseRoundCoachNotice(java.lang.
	 * String, java.lang.String, java.lang.String)
	 */
	public void sendCourseRoundCoachNotice(String mobile, String student, String courseName) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.FREEZE_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("studentName", student);
			values.put("courseName", courseName);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendCourseRoundCoachNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.fitstart.admin.handler.SmsSender#sendCoursewareNotice(java.lang.
	 * String)
	 */
	public void sendCoursewareNotice(String mobile) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.COURSEWARE_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendCourseRoundCoachNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendIndividualRoundFreezeNotice(co.
	 * fitstart.entity.course.round.CourseRound)
	 */
	public void sendIndividualRoundFreezeNotice(CourseRound courseRound) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.INDIVIDUAL_ROUND_FREEZE_NOTICE);

		if (template != null) {
			int courseAmount = (courseRound.getAttrs() == null
					|| courseRound.getAttrs().get(Constant.COURSE_AMOUNT) == null) ? courseRound.getCourse().getHours()
							: Integer.parseInt(courseRound.getAttrs().get(Constant.COURSE_AMOUNT).getValue());
			int restAmount = courseAmount - courseRound.getFinished();

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("courseAmount", courseAmount + "");
			values.put("restAmount", restAmount + "");
			values.put("holdDate", DateFormatUtils.format(courseRound.getHoldDate(), "yyyy/MM/dd"));
			values.put("holdEndDate", DateFormatUtils.format(courseRound.getHoldEndDate(), "yyyy/MM/dd"));

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(courseRound.getStudent().getMobile(), message);

			if (!result.isValid()) {
				logger.error("sendIndividualRoundCoachNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendYearRoundFreezeNotice(co.fitstart
	 * .entity.course.round.CourseRound)
	 */
	public void sendYearRoundFreezeNotice(CourseRound courseRound) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.YEAR_ROUND_FREEZE_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("holdDate", DateFormatUtils.format(courseRound.getHoldDate(), "yyyy/MM/dd"));
			values.put("holdEndDate", DateFormatUtils.format(courseRound.getHoldEndDate(), "yyyy/MM/dd"));

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(courseRound.getStudent().getMobile(), message);

			if (!result.isValid()) {
				logger.error("sendYearRoundCoachNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendIndividualFinishNotice(java.lang.
	 * String, java.lang.String, int)
	 */
	public void sendIndividualFinishNotice(String mobile, String site, int courseAmount) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.INDIVIDUAL_FINISH_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("site", site);
			values.put("amount", courseAmount + "");

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendIndividualFinishNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendIndividualDelayNotice(java.lang.
	 * String, java.lang.String, int, java.lang.String)
	 */
	public void sendIndividualDelayNotice(String mobile, String site, int courseAmount, String effectiveEnd) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.INDIVIDUAL_DELAY_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("site", site);
			values.put("amount", courseAmount + "");
			values.put("date", effectiveEnd);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendIndividualDelayNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendIndividualCancelNotice(java.lang.
	 * String, java.lang.String, int)
	 */
	public void sendIndividualCancelNotice(String mobile, String site, int courseAmount) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.STUDENT_INDIVIDUAL_CANCEL_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("site", site);
			values.put("amount", courseAmount + "");

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendIndividualCancelNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.fitstart.admin.handler.SmsSender#sendYearFinishNotice(java.lang.
	 * String, java.lang.String)
	 */
	public void sendYearFinishNotice(String mobile, String site) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.YEAR_FINISH_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("site", site);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendYearFinishNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendYearDelayNotice(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public void sendYearDelayNotice(String mobile, String site, String effectiveEnd) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.YEAR_DELAY_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("site", site);
			values.put("date", effectiveEnd);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendYearDelayNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.fitstart.admin.handler.SmsSender#sendYearCancelNotice(java.lang.
	 * String, java.lang.String)
	 */
	public void sendYearCancelNotice(String mobile, String site) {
		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.STUDENT_YEAR_CANCEL_NOTICE);

		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("site", site);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			if (!result.isValid()) {
				logger.error("sendYearCancelNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

	/*
	 * (non-Javadoc) 手动销课短信提醒
	 * 
	 * @see
	 * co.fitstart.admin.handler.SmsSender#sendOverSchedule(java.lang.String)
	 */
	public void sendOverSchedule(String mobile, String scheduleTime) {

		Result result = new Result();

		// Get message template
		String template = smsHandler.getShortMessageTemplate(MessageType.OVER_SCHEDULE_NOTICE);
		logger.info("# sendOverSchedule() template [{}] ", template);
		if (template != null) {

			Map<String, String> values = new HashMap<String, String>(2);
			values.put("company", company);
			values.put("scheduleTime", scheduleTime);

			String message = StringUtils.replacePlaceHolder(template, values, "#", "#");
			logger.info("# sendOverSchedule() message [{}] template [{}] ", message, template);

			// Send message
			result = smsHandler.sendShortMessage(mobile, message);

			logger.info("# sendOverSchedule() result [{}] mobile [{}] ", result, mobile);

			if (!result.isValid()) {
				logger.error("sendOverScheduleNotice() error", JsonUtils.toJsonString(result));
			}
		}
	}

}
